from datetime import datetime
from app.extensions import db

class StudyRecord(db.Model):
    """学习记录表模型"""
    __tablename__ = 'study_record'
    
    record_id = db.Column(db.Integer, primary_key=True, autoincrement=True, comment='记录唯一标识')
    study_time = db.Column(db.DateTime, default=datetime.now, comment='学习时间')
    study_type = db.Column(db.Enum('识字', '背默', name='study_types'), 
                          nullable=False, comment='学习类型')
    duration = db.Column(db.SmallInteger, nullable=False, comment='耗时（分钟）')
    mastery_score = db.Column(db.SmallInteger, nullable=False, comment='掌握度评分')
    
    # 逻辑外键字段
    student_id = db.Column(db.Integer, db.ForeignKey('student.student_id'), 
                          nullable=False, comment='学生ID')
    content_id = db.Column(db.Integer, db.ForeignKey('content.content_id'), 
                           nullable=False, comment='内容ID')

    def to_dict(self):
        return {
            'record_id': self.record_id,
            'study_time': self.study_time,
            'study_type': self.study_type,
            'duration': self.duration,
            'mastery_score': self.mastery_score,
            'student_id': self.student_id,
            'content_id': self.content_id
        }
    def __repr__(self):
        return f'<StudyRecord {self.record_id} ({self.study_type})>'